<?php

/**
 *  
 */
class AdminSiteClientController extends AdminBaseController
{

    public function beforeAction($action)
    {
        parent::beforeAction($action);

        if( !$this->permissionManager->checkRole('work_manager') ) {
            Request::redirect('admin/');
        }

        $this->menus->setFocusByAction('adminSiteClient/list');
        return true;
    }

    /**
     *  index
     */
    public function actionIndex()
    {
        Request::redirect('list');
    }

    /**
     *  list
     */
    public function actionList( $page=1, $searchKey='' )
    {
        $clients = new Clients();
        $myClients  = $clients->findClients( 0, $searchKey, $page );
        $rowCount = $clients->getNumByFindClients( 0, $searchKey );

        $this->pageLimit = new PageLimitManager();
        $this->pageLimit->setBaseUrl( $this->createUrl('') );
        $this->pageLimit->setRowCount( $rowCount );
        $this->pageLimit->setPage( $page );
        $this->pageLimit->setSearchKey( $searchKey );

        $this->render('list',array(
            'clients' => $myClients, 
        ));
    }


    public function actionEdit( $id )
    {

        $id = (int) $id;
        $clients = new Clients();
        $client = $clients->getClient($id);
        if(!$client) {
            Request::redirect('list');
        }

        // update
        if( Request::isPost() ) {
            $posts = Request::getPostInfo();
            $client->setName(           $posts['name']      );
            $client->setEmail(          $posts['email']     );
            $client->setTel(            $posts['tel']       );
            $client->setPhone(          $posts['phone']     );
            $client->setAddressNumber(  $posts['addressNumber'] );
            $client->setAddress(        $posts['address']   );
            $client->setBirthday (      strtotime($posts['birthday'])  );
            $client->setMemo(           $posts['memo']      );
            $client->setUpdateDate(     time()              );

            $messages = $client->validate();
            $client->filter();
            if( $messages ) {
                SessionManager::setErrorMessages('更新失敗');
                $data['fieldsMessages'] = $messages;
                $data['client'] = $client;
                $this->render('edit', $data );
                return;
            }
            $clients = new Clients();
            $clients->updateClient($client);
            SessionManager::setSuccessMessages('更新成功');

        }

        // edit
        $data['client'] = $client;
        $this->render('edit',$data);

    }


    public function actionNew()
    {

        // add 
        if( Request::isPost() ) {
            $posts = Request::getPostInfo();
            $client = new Client();
            $client->setName(           $posts['name']      );
            $client->setEmail(          $posts['email']     );
            $client->setTel(            $posts['tel']       );
            $client->setPhone(          $posts['phone']     );
            $client->setAddressNumber(  $posts['addressNumber'] );
            $client->setAddress(        $posts['address']   );
            $client->setBirthday (      strtotime($posts['birthday'])  );
            $client->setMemo(           $posts['memo']      );

            $messages = $client->validate();
            $client->filter();
            if( $messages ) {
                SessionManager::setErrorMessages('新增失敗');
                $data['fieldsMessages'] = $messages;
                $data['client'] = $client;
                $this->render('new', $data );
                return;
            }
            $clients = new Clients();
            $clients->addClient($client);
            SessionManager::setSuccessMessages('新增成功');

        }

        // new
        $data['client'] = new Client();
        $this->render( 'new', $data );

    }

    /* ================================================================================
        客戶眷屬 以下
    ================================================================================ */

    /**
     *  checkDependantParent
     *  success: 
     *      @return client
     *
     *  fail:
     *      redirect to some url
     *
     */
    protected function _checkDependantParent( $clientId )
    {
        if( $clientId<=0 ) {
            Request::redirect('list');
        }

        $clients = new Clients();
        $client = $clients->getClient($clientId);
        if(!$client) {
            Request::redirect('list');
        }
        return $client;

    }

    public function actionListDependant( $parentId, $page=1, $searchKey='' )
    {
        $clientId = (int) $parentId;
        $client = $this->_checkDependantParent( $clientId );

        $clients = new Clients();
        $subClients = $clients->getClientDependantsById( $clientId );

        $this->render('listDependant',array(
            'client' => $client,
            'subClients' => $subClients
        ));
    }

    public function actionNewDependant( $parentId )
    {
        $clientId = (int) $parentId;
        $client = $this->_checkDependantParent( $clientId );

        // add 
        if( Request::isPost() ) {
            $posts = Request::getPostInfo();
            $client = new Client();
            $client->setClientId(       $clientId           );
            $client->setName(           $posts['name']      );
            $client->setTel(            $posts['tel']       );
            $client->setPhone(          $posts['phone']     );
            $client->setBirthday (      strtotime($posts['birthday'])  );
            $client->setMemo(           $posts['memo']      );

            $messages = $client->validate();
            $client->filter();
            if( $messages ) {
                SessionManager::setErrorMessages('新增眷屬失敗');
                $data['fieldsMessages'] = $messages;
                $data['client'] = $client;
                $this->render('newDependant', $data );
                return;
            }
            $clients = new Clients();
            $clients->addClient($client);
            SessionManager::setSuccessMessages('新增眷屬成功');

        }

        // new
        $data['client'] = new Client();
        $this->render( 'newDependant', $data );
    }


    public function actionEditDependant( $parentId, $id )
    {
        $parentClientId = (int) $parentId;
        $id             = (int) $id;
        $client = $this->_checkDependantParent( $id );
        if( $client->getClientId() != $parentClientId ) {
            Request::redirect('list');
        }

        // update 
        if( Request::isPost() ) {
            $posts = Request::getPostInfo();
            $client->setName    ( $posts['name']      );
            $client->setTel     ( $posts['tel']       );
            $client->setPhone   ( $posts['phone']     );
            $client->setBirthday( strtotime($posts['birthday'])  );
            $client->setMemo    ( $posts['memo']      );

            $messages = $client->validate();
            $client->filter();
            if( $messages ) {
                SessionManager::setErrorMessages('更新眷屬失敗');
                $data['fieldsMessages'] = $messages;
                $data['client'] = $client;
                $this->render('editDependant', $data );
                return;
            }
            
            $clients = new Clients();
            $clients->updateClient($client);
            SessionManager::setSuccessMessages('更新眷屬成功');

        }

        // edit
        $data['client'] = $client;
        $this->render( 'editDependant', $data );
    }

}
